#include<iostream>
#include"/Users/yjc/Programming/eurekas-algorithm-path/2.Linklist/Linklist.cpp"


ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) 
{
    if(!headA||!headB)//为解决直接传入两个空链表的情况
        return nullptr;
    ListNode *afast=headA,*aslow=headA,*bfast=headB,*bslow=headB;
    //让较长链表的快指针位置到达与较短链长度一样长的位置
    while(afast->next&&bfast->next)
    {
        afast=afast->next;
        bfast=bfast->next;
    }
    //同时移动较长链的快慢指针，间隔还是较短链的长度，直到到达长链末尾
    if(!afast->next)
    {
        while(bfast->next)
        {
            bslow=bslow->next;
            bfast=bfast->next;
        }
        
    }
    else
    {
        while(afast->next)
        {
            aslow=aslow->next;
            afast=afast->next;
        }
    }
    //开始比较
    while(bslow!=aslow&&bslow->next)
    {
        bslow=bslow->next;
        aslow=aslow->next;
    }
    
    if(bslow==aslow)
        return bslow;
    return nullptr;   
    
}
